package views.cad;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Usuario;
import supers.GUICadInterface;
import supers.GUIInternalFrame;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class CadUsuGUI extends GUIInternalFrame implements GUICadInterface{
	public JTextField tfLogin;
	public JPasswordField tfSenha;
	public JPasswordField tfSenha2;
	public JPanel panel;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JTextField tfNome;
	public JFormattedTextField tfCod;
	public JRadioButton rbUsu;
	public JRadioButton rbAdmin;
	
	public CadUsuGUI() {
		setTitle("Cadastro de Usu\u00E1rios");
		setSize(new Dimension(330, 257));
		
		tfLogin = new JTextField();
		tfLogin.setBounds(89, 87, 89, 20);
		getContentPane().add(tfLogin);
		tfLogin.setColumns(10);
		try {
			tfSenha = new JPasswordField(6);
			tfSenha.setBounds(89, 117, 89, 20);
			getContentPane().add(tfSenha);
			
			JLabel lblNewLabel = new JLabel("Senha:");
			lblNewLabel.setLabelFor(tfSenha);
			lblNewLabel.setBounds(26, 121, 34, 14);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Login:");
			lblNewLabel_1.setLabelFor(tfLogin);
			lblNewLabel_1.setBounds(26, 91, 46, 14);
			getContentPane().add(lblNewLabel_1);
			
			panel = new JPanel();
			panel.setLayout(null);
			panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel.setBounds(26, 179, 268, 26);
			getContentPane().add(panel);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel.add(btListar);
			
			JLabel label = new JLabel("Cod.:");
			label.setBounds(26, 29, 34, 14);
			getContentPane().add(label);
			
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(89, 25, 30, 20);
			getContentPane().add(tfCod);
			
			JLabel label_1 = new JLabel("Nome:");
			label_1.setBounds(26, 60, 46, 14);
			getContentPane().add(label_1);
			
			tfNome = new JTextField();
			tfNome.setColumns(10);
			tfNome.setBounds(89, 56, 205, 20);
			getContentPane().add(tfNome);
			
			rbUsu = new JRadioButton("Usu\u00E1rio");
			rbUsu.setSelected(true);
			rbUsu.setBounds(198, 88, 96, 23);
			getContentPane().add(rbUsu);
			
			rbAdmin = new JRadioButton("Administrador");
			rbAdmin.setBounds(198, 114, 96, 23);
			getContentPane().add(rbAdmin);
			
			JLabel lblConfirme = new JLabel("Confirme:");
			lblConfirme.setBounds(26, 152, 53, 14);
			getContentPane().add(lblConfirme);
			
			tfSenha2 = new JPasswordField(6);
			tfSenha2.setBounds(89, 148, 89, 20);
			getContentPane().add(tfSenha2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfCod.addFocusListener(fl);
        tfSenha.addFocusListener(fl);
        tfSenha2.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);
        rbUsu.addActionListener(ac);
        rbAdmin.addActionListener(ac);
	}

	@Override
	public void limpar(){
		tfCod.setText("");
		tfNome.setText("");
		tfLogin.setText("");
		tfSenha.setText("");
		tfSenha2.setText("");
		btExcluir.setEnabled(false);
        rbUsu.setSelected(true);
        rbAdmin.setSelected(false);
	}

	public void setDados(Object obj){
		Usuario usu = (Usuario) obj;
		tfCod.setText(usu.getCodigo());
		tfNome.setText(usu.getNome());
		tfLogin.setText(usu.getLogin());
		tfSenha.setText(usu.getSenha());
		tfSenha2.setText(usu.getSenha());
        rbUsu.setSelected(false);
        rbAdmin.setSelected(false);
		if(usu.getTipo() == 0)
			rbUsu.setSelected(true);
		else
			rbAdmin.setSelected(true);
	}
	
	public Usuario getDados(){
		Usuario usu = new Usuario();
		usu.setCodigo(tfCod.getText());
		usu.setNome(tfNome.getText());
		usu.setLogin(tfLogin.getText());
		usu.setSenha(GlobalDeclarations.charToString(tfSenha.getPassword()));
		int tipo = 0;
		if(rbAdmin.isSelected())
			tipo = 1;
		usu.setTipo(tipo);
		usu.setDtOpe(new Date(System.currentTimeMillis()));
		return usu;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}
